package com.ec.fb.controller;

import com.ec.common.domain.PageVO;
import com.ec.common.domain.Result;
import com.ec.common.utils.UserContext;
import com.ec.fb.domain.dto.FeedbackCreateRequestDTO;
import com.ec.fb.domain.dto.FeedbackReplyRequestDTO;
import com.ec.fb.domain.dto.FeedbackResponseDTO;
import com.ec.fb.domain.dto.FeedbackStatsDTO;
import com.ec.fb.service.FeedbackService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/feedbacks")
public class FeedbackController {
    @Resource
    private FeedbackService feedbacksService;

    @ApiOperation("提交反馈")
    @PostMapping("/submit")
    public Result submitFeedback(@RequestBody FeedbackCreateRequestDTO requestDTO) {
        System.out.println("当前用户：" + UserContext.getUser());
        Long user = UserContext.getUser();
        return Result.ok(feedbacksService.submitFeedback(requestDTO, user));
    }

    @ApiOperation("回复反馈")
    @PostMapping("/reply")
    public Result replyFeedback(FeedbackReplyRequestDTO requestDTO) {
        Integer adminId = Math.toIntExact(UserContext.getUser());
        return Result.ok(feedbacksService.replyFeedback(requestDTO, adminId));
    }

    @ApiOperation("获取反馈列表")
    @GetMapping("/list")
    public Result<PageVO<FeedbackResponseDTO>> listFeedbacks(@RequestParam(defaultValue = "1") Integer pageNum,
                                                             @RequestParam(defaultValue = "10") Integer pageSize,
                                                             @RequestParam(required = false) Integer rating) {
        return Result.ok(feedbacksService.listFeedbacks(pageNum, pageSize, rating));
    }

    @ApiOperation("获取管理员反馈列表")
    @GetMapping("/admin")
    public Result<PageVO<FeedbackResponseDTO>> listAdminFeedbacks(@RequestParam(defaultValue = "1") Integer pageNum,
                                                                  @RequestParam(defaultValue = "10") Integer pageSize,
                                                                  @RequestParam(required = false) String status,
                                                                  @RequestParam(required = false) String userQuery) {
        return Result.ok(feedbacksService.listAdminFeedbacks(pageNum, pageSize, status, userQuery));
    }

    @ApiOperation("获取用户反馈列表")
    @GetMapping("/my")
    public Result<PageVO<FeedbackResponseDTO>> listMyFeedbacks(@RequestParam Integer pageNum,
                                                               @RequestParam Integer pageSize,
                                                               @RequestParam Long userId) {
        return Result.ok(feedbacksService.listMyFeedbacks(pageNum, pageSize, userId));
    }


    @ApiOperation("获取反馈统计信息")
    @GetMapping("/stats")
    public Result<FeedbackStatsDTO> getFeedbackStats() {
        return Result.ok(feedbacksService.getFeedbackStats());
    }
}
